# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.core.paginator import Paginator
from django.http import HttpResponseRedirect
from django.shortcuts import render

# Create your views here.
from name.forms import LoginForm,RegisterForm
from name.models import CommonUser, Written
from datetime import datetime


def login(request):
    if request.method == 'POST':
        user_form = LoginForm(request.POST)
        if user_form.is_valid():
            username = user_form.cleaned_data['username']
            password = user_form.cleaned_data['password']
            user = CommonUser.objects.filter(username=username,password=password)
            if user:
                response = HttpResponseRedirect('/name/index')
                response.set_cookie('username',username)
                return response
            else:
                c = {'login_form':user_form}
                return render(request,template_name='login.html',context=c)
    else:
        login_form = LoginForm()
        c = {'login_form':login_form}
        return render(request,'login.html',c)

def board(request):
    if request.method == 'POST':
        written = Written.objects.last()
        print written
        if written == None:
            title = request.POST['title']
            content = request.POST['content']
            submit_time = datetime.now()
            print submit_time
            cook = request.COOKIES.get('username')
            user = CommonUser.objects.get(username=cook)
            print type(user)
            w1 = Written(title=title,content=content,submit_time=submit_time,profile_id=user)
            w1.save()
            # Written.objects.create(title=title,content=content,submit_time=submit_time,profile_id=profile_id)
            print '成功'
            return render(request,'board.html')
        else:
            create_time = written.submit_time
            now_time = datetime.now()
            print create_time
            print now_time
            time_len = (now_time - create_time).seconds
            frequency = 60*5
            if time_len < frequency:
                return render(request,'board.html',{'error':'操作繁忙请稍后！！！'})
            else:
                title = request.POST['title']
                content = request.POST['content']
                submit_time = datetime.now()
                cook = request.COOKIES.get('username')
                user = CommonUser.objects.get(username=cook)
                print type(user)
                w1 = Written(title=title, content=content, submit_time=submit_time, profile_id=user)
                w1.save()
                # Written.objects.create(title=title,content=content,submit_time=submit_time,profile_id=profile_id)
                print '成功'
                return render(request, 'board.html')

    else:
        cook = request.COOKIES.get('username')
        c = {'cook':cook}
        return render(request,'board.html',c)

def index(request):
    cook = request.COOKIES.get('username')
    c = {'cook': cook}
    return render(request, 'index.html', c)

def qwe(request):
    cook = request.COOKIES.get('username')
    user = CommonUser.objects.get(username=cook)
    boardls =  Written.objects.filter(profile_id=user)
    paginator = Paginator(boardls,1)
    page = request.GET.get('page')
    try:
        post_list = paginator.page(page)
    except:
        post_list = paginator.page(1)

    cook = request.COOKIES.get('username')

    return render(request,'boardlist.html',{'posts':post_list,'cook': cook})


def logout(request):

    response = HttpResponseRedirect('/name/login')
    response.delete_cookie('username')
    return response


def register(request):
    if request.method == 'POST':
        user_form = RegisterForm(request.POST)
        if user_form.is_valid():
            username = user_form.cleaned_data['username']
            password = user_form.cleaned_data['password']
            email = user_form.cleaned_data['email']
            user = CommonUser(username=username,password=password,email=email)
            user.save()
            print '1354654'
            response = HttpResponseRedirect('/name/login')
            return response
    else:
        login_form = RegisterForm()
        c = {'login_form': login_form}
        return render(request, 'register.html', c)